package com.come4loves.game.module.fight.helper;

import java.util.Collection;
import java.util.List;

import com.come4loves.game.module.fight.action.BatchAction;
import com.come4loves.game.module.fight.action.BuffAction;
import com.come4loves.game.module.fight.action.CardToLocationAction;
import com.come4loves.game.module.fight.constants.BuffType;
import com.come4loves.game.module.fight.constants.CardLocation;
import com.come4loves.game.module.fight.model.ActorBattle;
import com.come4loves.game.module.fight.model.BuffData;
import com.come4loves.game.module.fight.model.FightCard;
import com.come4loves.game.module.skill.model.DamageModel;

public class FightHelper {

	public static void createDeadAction(FightCard fightCard, BatchAction batchAction) {
		if (fightCard.isSummon()) {
			CardToLocationAction cardToLocationAction = CardToLocationAction.createFromCard(fightCard, CardLocation.OUTSIDEGAME);
			batchAction.addAction(cardToLocationAction);
		} else {
			CardToLocationAction cardToLocationAction = CardToLocationAction.createFromCard(fightCard, CardLocation.DISCARD);
			batchAction.addAction(cardToLocationAction);
			
		}
	}
	public static void createDeadAction(FightCard fightCard, ActionRecorder actionRecorder) {
		if (fightCard.isSummon()) {
			CardToLocationAction cardToLocationAction = CardToLocationAction.createFromCard(fightCard, CardLocation.OUTSIDEGAME);
			actionRecorder.addAction(cardToLocationAction);
		} else {
			CardToLocationAction cardToLocationAction = CardToLocationAction.createFromCard(fightCard, CardLocation.DISCARD);
			actionRecorder.addAction(cardToLocationAction);
			
		}
	}
	
	/**
	 * 通过卡牌获得角色
	 * 
	 * @param fightCard
	 * @return
	 */
	public static ActorBattle getActorBattle(FightCard fightCard, Collection<ActorBattle> allActorBattles) {
		for (ActorBattle actorBattle : allActorBattles) {
			for (FightCard card : actorBattle.getAllCards()) {
				if (card.getId() == fightCard.getId()) {
					return actorBattle;
				}
			}
		}
		return null;
	}
	
	/**
	 * 通过id查找卡牌
	 * 
	 * @param id
	 * @return
	 */
	public static FightCard serchCardInBattle(int id, Collection<ActorBattle> allActorBattles) {
		for (ActorBattle actorBattle : allActorBattles) {
			if (actorBattle.getShowCards().containsKey(id)) {
				return actorBattle.getShowCards().get(id);
			}
			for (FightCard card : actorBattle.getMainCards()) {
				if (card.getId() == id){
					return card;
				}
			}
		}
		return null;
	}
	
	public static DamageModel realDamage(FightCard fightCard, int value, boolean pDamage, ActionRecorder actionRecorder) {
		int realDamage = value;
		if (fightCard.isBuffAble(BuffType.HOLYSHIELD)){
			BuffData bd = fightCard.getBuff(BuffType.HOLYSHIELD).get(0);
			bd.setBuffValue(bd.getBuffValue() - 1);
			if (bd.getBuffValue() == 0) {
				fightCard.removeBuff(bd);
				BuffAction buffAction = new BuffAction(fightCard, bd, 0);
				actionRecorder.addAction(buffAction);
			}
			realDamage = 0;
		} else {
			if (pDamage) {
				if (fightCard.isBuffAble(BuffType.PDAMAGEREDUCE)) {
					List<BuffData> bufList = fightCard.getBuff(BuffType.PDAMAGEREDUCE);
					int reduceValue = 0;
					for (BuffData buffData : bufList) {
						reduceValue += buffData.getBuffValue();
					}
					
					realDamage += reduceValue;
				}
			} else {
				if (fightCard.isBuffAble(BuffType.MDAMAGEREDUCE)) {
					List<BuffData> bufList = fightCard.getBuff(BuffType.MDAMAGEREDUCE);
					int reduceValue = 0;
					for (BuffData buffData : bufList) {
						reduceValue += buffData.getBuffValue();
					}
					
					realDamage += reduceValue;
				}
			}
		}
		realDamage = realDamage > 0?realDamage : 0;
		int beAttakerHp = fightCard.getHp() - realDamage;
		beAttakerHp = beAttakerHp > 0 ? beAttakerHp : 0;
		fightCard.setHp(beAttakerHp);
		DamageModel damageModel = new DamageModel(realDamage, false);
		return damageModel;
	}
	
	

}
